<template>
  <div>
    <el-dialog
      :title="landingpage_name"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      class="mt-dialog"
      width="80%"
      @close="close"
    >

      <div class="top">
        <el-input
          v-model="kwInput"
          placeholder="输入节点名称过滤"
          class="input-with-select"
          clearable
          @keyup.native.enter="handleSearch"
        >
          <el-button slot="append" icon="el-icon-search" @click="handleSearch">查询状态</el-button>
        </el-input>
        <el-button
          type="success"
          title="增加"
          icon="el-icon-plus"
          @click="handleAction('create', '增加')"
        >新增
        </el-button>
      </div>

      <el-table
        v-loading.fullscreen.lock="listLoading"
        :data="data"
        border
        fit
        highlight-current-row
        style="width: 100%"
        @sort-change="handleSort"
      >
        <el-table-column align="center" label="缩略图" prop="thumbnail" sortable="custom">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.thumbnail.length"
              style="width: 100px; height: 100px"
              :src="scope.row.thumbnail"
              :title="scope.row.thumbnail"
              :preview-src-list="[scope.row.thumbnail]"
            />
          </template>
        </el-table-column>
        <el-table-column align="center" label="名称" prop="name" sortable="custom" />
        <el-table-column align="center" label="被替换内容" prop="content" sortable="custom" />
        <el-table-column align="center" label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-show="!scope.row.edit"
              size="mini"
              icon="el-icon-delete"
              type="danger"
              plain
              @click="handleAction('edit', scope.row)"
            >修改
            </el-button>
            <el-button
              v-show="!scope.row.edit"
              size="mini"
              icon="el-icon-delete"
              type="error"
              plain
              @click="handleAction('delete', scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog
      :title="title"
      :visible.sync="innerDialogVisible"
      width="60%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form ref="dataForm" :model="temp" label-position="left" label-width="200px">
        <el-form-item label="name">
          <el-input
            v-model="temp.name"
            placeholder="替换节点名称(请联系专业人员来操作)"
            clearable
          />
        </el-form-item>
        <el-form-item label="被替换的内容">
          <el-input
            v-model="temp.content"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 20}"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="修改点截图">
          <div class="paste-area" @paste="handlePaste">
            <span>点击这里并粘贴图片</span>
            <div v-if="imageUrl">
              <img style="width: 100px; height: 100px" :src="imageUrl" alt="粘贴的图片">
            </div>
            <button v-if="imageUrl" @click="imageUrl=null">清空图片</button>
          </div>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button :type="action !== 'delete'?'primary':'danger'" @click="confirm">确 定</el-button>
        <el-button @click="innerDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/landingpage/lpnode'
import qs from 'qs'
import Pasteuploadimage from '../../../components/Upload/pasteuploadimage'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}
export default {
  name: 'Lpnode',
  components: { Pasteuploadimage },
  props: ['landingpage_id', 'landingpage_name', 'showDialog'],
  data() {
    return {
      data: [],
      listLoading: true,
      kwInput: '',
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,

      action: null,
      dialogVisible: false,
      title: '修改节点',
      temp: {
        landingpage_id: '',
        name: '',
        selector: '',
        xpath: '',
        content: '',
        file: null
      },
      imageUrl: null,
      width: '',
      innerDialogVisible: false
    }
  },
  watch: {
    landingpage_id(v) {
      if (v) {
        this.getList()
      }
    },
    showDialog(v) {
      this.dialogVisible = v
      console.log('show dialog')
    }
  },
  methods: {
    readFileAsBase64(file) {
      const reader = new FileReader()
      reader.onload = (e) => {
        this.imageUrl = e.target.result
        this.temp.file = this.imageUrl
      }
      reader.readAsDataURL(file)
    },
    handlePaste(event) {
      const items = (event.clipboardData || event.originalEvent.clipboardData).items
      for (const item of items) {
        if (item.type.indexOf('image') === 0) {
          const file = item.getAsFile()
          this.readFileAsBase64(file)
          // this.temp.file = file
          // this.imageUrl = URL.createObjectURL(file);
          // console.log(this.imageUrl)
        }
      }
    },
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        landingpage_id: this.landingpage_id,
        pagesize: this.pagesize,
        page: this.page,
        order_by: this.order_by,
        order_desc: this.order_desc,
        wp_account_id: this.wp_account_id
      }))
        .then(response => {
          this.scrollTop()
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(e => {
          this.listLoading = false
        })
    },
    handleAction(action, row) {
      this.action = action
      this.temp.landingpage_id = this.landingpage_id
      switch (action) {
        case 'create':
          this.innerDialogVisible = true
          this.title = row
          break
        case 'edit':
          this.temp.id = row.id
          this.temp.landingpage_id = row.landingpage_id
          this.temp.name = row.name
          this.temp.xpath = row.xpath
          this.temp.content = row.content
          this.temp.selector = row.selector
          this.title = '修改' + row.name
          this.innerDialogVisible = true
          break
        case 'delete':
          this.dialogVisible = true
          this.title = `确认要删除${row.wpaccount_domain}模板${row.name}?`
          this.temp = row
          break
      }
    },
    confirm() {
      const _data = this.temp
      switch (this.action) {
        case 'create':
          Post(_data).then(response => {
            this.dialogVisible = false
            this.getList()
            this.notify('添加成功')
          })
          return
        case 'edit':
          Put(_data.id, _data).then(response => {
            this.dialogVisible = false
            this.getList()
            this.notify('更新成功')
          })
          return
        case 'delete':
          Delete(_data.id).then((response) => {
            this.dialogVisible = false
            this.getList()
            this.notify('删除成功')
          })
          return
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.searchModule()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.handleSearch()
    },
    // 点击搜索
    handleSearch() {
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.handleSearch()
    },
    scrollTop() {
      window.scrollTo(0, 0)
    },
    close() {
      this.$emit('closeMyDialog', false)
    }
  }
}
</script>

<style scoped>

.paste-area {
  border: 2px dashed #ccc;
  padding: 20px;
  text-align: center;
}
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .top >>> .el-select .el-input {
    width: 350px;
  }

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .top {
    padding-left: 20px;
    margin-bottom: 30px;
    margin-top: 28px;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .add-btn {
    float: right;
    margin-right: 20px;
    width: 150px
  }

  .mt-pagination {
    float: right;
    margin-top: 5px;
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 68px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin-top: 60px;
    /*margin: 168px auto !important;*/
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 100px;
  }
</style>
